<?php
namespace db;

class database {
    private $db;
    private $dbUser;
    private $dbPass;
    private $dbName;

    function __construct() {
        $this->dbUser = "root";
        $this->dbPass = "";
        $this->dbName = "teran";

        $this->db = mysqli_connect ( 'localhost', $this->dbUser, $this->dbPass );
        mysqli_select_db($this->db, $this->dbName );
    }

    function execSql($sSql){
        try {
            $res = mysqli_query( $this->db,$sSql );

            if (!$res){
                $this->saveError($this->db->error." - Query: " . $sSql);
            }
        }catch (\mysqli_sql_exception $e) {
            $this->saveError($e->getCode() . " - " . $e->getMessage());
            $res = false;
        }

        return $res;
    }

    private function saveError($sError){
        global $config;
        $sSql = "insert into `".$config["dbName"]."`.`errores` (`error`,`fecha`) values('".mysqli_real_escape_string($this->db,$sError)."','".date("YmdHis")."')";
        $this->execSql($sSql);
    }

    /**
     * @return string
     */
    public function getDbName()
    {
        return $this->dbName;
    }

    public function getInsertedId(){
        return mysqli_insert_id($this->db);
    }


}